﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Xnatlas.Demo
{
    public class DemoGame : Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;
        private GraphicsDeviceAtlas _atlas;
        private AtlasedTexture _fox;
        private AtlasedTexture _penny;

        public DemoGame()
            : base()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
#if (DEBUG)
            // Only do this for during development!
            CreateAtlas();
#endif
            
            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            _atlas = new GraphicsDeviceAtlas(Content, "atlas", StorageType.Json);
            LoadTextures(_atlas);
        }

        protected void CreateAtlas()
        {
            BitmapAtlas bitmapAtlas = new BitmapAtlas(512, 512, @"..\..\..\Content\");
            LoadTextures(bitmapAtlas);
            bitmapAtlas.AppendQueuedTextures();
            bitmapAtlas.SaveToFile("atlas", StorageType.Json);

            // Now copy the new atlas.png & atlas.xml to the actual target
        }

        protected void LoadTextures(BaseAtlas atlas)
        {
            _fox = atlas.GetOrEnqueueTexture(@"FoxRanger");
            _penny = atlas.GetOrEnqueueTexture(@"penny the penguin");
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            _spriteBatch.Begin();
            _spriteBatch.Draw(_atlas.Texture, new Rectangle(100, 100, _fox.AtlasAreaRectangle.Width, _fox.AtlasAreaRectangle.Height), _fox.AtlasAreaRectangle, Color.White);
            _spriteBatch.Draw(_atlas.Texture, new Rectangle(150, 150, _penny.AtlasAreaRectangle.Width, _penny.AtlasAreaRectangle.Height), _penny.AtlasAreaRectangle, Color.White);
            _spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
